package cn.marc.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class EdenDataBase {
	private Connection connection = null;
	private Statement statement = null;
	private ResultSet resultSet = null;
	private String sql = "SELECT * FROM user_table";
	private boolean isSelect = true;
	public String pw = "";

	public EdenDataBase() {
		
	}
	
	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

	public boolean isSelect() {
		return isSelect;
	}

	public void setSelect(boolean isSelect) {
		this.isSelect = isSelect;
	}



	public boolean insert(String account, String password, String nickname) {
		if (account==null || password==null || nickname==null) {
			System.out.println("account==null || password==null || nickname==null");
			return false;
			
		} else {
			sql = "INSERT INTO user_table (account_user_table, user_table_password, "
					+ "user_table_status, user_table_nickname) "
					+ "VALUES ('"+account+"', '"+password+"', 'offline', '"+nickname+"');";
			isSelect = false;
			return true;
			
		}
		
	}
	
	public boolean update(String account, String key, String value) {
		if (key!="user_table_password" && key!="user_table_status" && key!="user_table_nickname" && 
				key!="user_table_detail" && key!="user_table_face") {
			System.out.println("key!=user_table_password && key!=user_table_status && "
					+ "key!=user_table_nickname && key!=user_table_detail && key!=user_table_face");
			return false;
			
		} else {
			sql = "UPDATE user_table SET "+key+"='"+value+"' WHERE account_user_table='"+account+"';";
			isSelect = false;
			return true;
			
		}
		
	}
	
	public boolean select() {
		sql = "SELECT * FROM user_table";
		isSelect = true;
		return true;
		
	}
	
	public ResultSet selectFace() {
		ResultSet rs = null;
		sql = "SELECT user_table_face FROM user_table";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://140.143.35.110:3306/eden_schema", 
					"root", "hao3bi");
			statement = connection.createStatement();
			if (isSelect) {
				resultSet = statement.executeQuery(sql);
				rs = resultSet;
				
			} else {
				System.out.println(sql);
				statement.executeUpdate(sql);
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			
		} finally {
			try {
				if(resultSet!=null) {
					resultSet.close();
					
				}
				
			} catch (Exception e2) {
				e2.printStackTrace();
				
			}
			
			try {
				statement.close();
				
			} catch (Exception e3) {
				e3.printStackTrace();
				
			}
			
			try {
				connection.close();
				
			} catch (Exception e4) {
				e4.printStackTrace();
				
			}
			
		}
		return rs;
	}
	
	public boolean execute() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://140.143.35.110:3306/eden_schema", 
					"root", "hao3bi");
			statement = connection.createStatement();
			if (isSelect) {
				resultSet = statement.executeQuery(sql);
				
				while(resultSet.next()) {
					pw = resultSet.getString("user_table_password");
//					System.out.println("id: "+resultSet.getInt("id_user_table"));
//					System.out.println("account: "+resultSet.getString("account_user_table"));
//					System.out.println("password: "+resultSet.getString("user_table_password"));
//					System.out.println("status: "+resultSet.getString("user_table_status"));
//					System.out.println("nickname: "+resultSet.getString("user_table_nickname"));
//					System.out.println("detail: "+resultSet.getString("user_table_detail"));
//					System.out.println("face: "+resultSet.getString("user_table_face"));
				}
				
			} else {
				System.out.println(sql);
				statement.executeUpdate(sql);
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
			
		} finally {
			try {
				if(resultSet!=null) {
					resultSet.close();
					
				}
				
			} catch (Exception e2) {
				e2.printStackTrace();
				return false;
				
			}
			
			try {
				statement.close();
				
			} catch (Exception e3) {
				e3.printStackTrace();
				return false;
				
			}
			
			try {
				connection.close();
				
			} catch (Exception e4) {
				e4.printStackTrace();
				return false;
				
			}
			
		}
		return true;
		
	}
	
}
